using System.Collections;
using System.Collections.Generic;
using GoogleMobileAds.Api;
using UnityEngine;
#if UNITY_PURCHASING
using UnityEngine.Purchasing;
#endif

namespace HWGames.Bundles.Internal.Analytics {
    public class SingularMgr : Singleton<SingularMgr> {

        public void Init() {
            GameObject singularGO = new GameObject("SingularSDKObject");
            singularGO.AddComponent<SingularSDK>();
            AdsHandler.Instance.OnRVPaidEvent += OnRewardPaidEvent;
            AdsHandler.Instance.OnInterPaidEvent += OnInterPaidEvent;
            AdsHandler.Instance.OnBannerPaidEvent += OnBannerPaidEvent;
            bool result = SingularSDK.SetGlobalProperty("app version", Application.version, true);
            HWLog.Log("Singular", "SetGlobalProperty " + result);
            InitMsg();
        }

        public void Clear() {
            ClearMsg();
        }

        private void InitMsg() {
        }

        private void ClearMsg() {
        }

        public void LogEvent(string eventStr) {
            SingularSDK.Event(eventStr);
        }

        public void LogEvent(string eventStr, Dictionary<string, object> args) {
            SingularSDK.Event(args, eventStr);
        }

        private void OnInterPaidEvent(AdValue adValue, InterstitialAd interAd) {
            HandleAdPaidEvent(interAd.GetResponseInfo().GetMediationAdapterClassName(), adValue);
        }

        private void OnRewardPaidEvent(AdValue adValue, RewardedAd rewardedAd) {
            HandleAdPaidEvent(rewardedAd.GetResponseInfo().GetMediationAdapterClassName(), adValue);
        }

        private void OnBannerPaidEvent(AdValue adValue, BannerView bannerView) {
            HandleAdPaidEvent(bannerView.GetResponseInfo().GetMediationAdapterClassName(), adValue);
        }

        private void HandleAdPaidEvent(string networkName, AdValue adValue) {
            SingularAdData data = new SingularAdData("Admob", adValue.CurrencyCode, adValue.Value / 1000000f);
            data.WithNetworkName(networkName);
            SingularSDK.AdRevenue(data);
        }

#if UNITY_PURCHASING
        public void InAppPurchase(Product product, Dictionary<string, object> attributes, bool isRestored = false) {
            SingularSDK.InAppPurchase(product, attributes, isRestored);
        }
#endif
    }
}